/******************************************************************************
 * The contents of this file are subject to the   JDI License  Version 1.0
 * ("License"); You may not use this file except in compliance with the License
 * Software distributed under the License is distributed on an  "AS IS"  basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 * The Initial Developer of the Original Code is 陈慧然. Portions created by 
 * 陈慧然 are Copyright 陈慧然.
 * All Rights Reserved.
 *
 *              Contact john_holen@163.com for licensing alternatives.
 *
 *****************************************************************************/


package org.peony.client.web.tools;


import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.peony.client.web.components.WebComponents;
import org.peony.client.web.components.table.Caption;
import org.peony.client.web.components.table.TBody;
import org.peony.client.web.components.table.TFoot;
import org.peony.client.web.components.table.THead;
import org.peony.client.web.components.table.Table;
import org.peony.client.web.components.table.Td;
import org.peony.client.web.components.table.Th;
import org.peony.client.web.components.table.Tr;
import org.peony.client.web.components.word.Word;
import org.peony.models.MCol;
import org.peony.standard.unvarying.client.web.IdsAndCssClassName;
import org.peony.tools.StringUtil;


/**
 *  将数据以Html Table的形式展现。
 *
 *  @author     陈慧然
 *  @version    Id: DateToTable.java, v 0.000 2008-4-25 下午05:12:54 陈慧然 Exp
 */
public class DataToTable {
	/**
	 * 将数据以Table的形式展现
	 * @param records 要被展现的数据（记录集）
	 * @param cols 列信息
	 * @param title Table的展现表名
	 * @param footer Table的地脚
	 * @return
	 * @throws DataTableException
	 */
	public static Table dataToTable(List<Map<String,Object>> records,List<MCol> cols,String title,Map<String,Object> footer) throws DataTableException{
		return dataToTableBase(records, cols, title, footer);
	}
	
	private static Table dataToTableBase(List<Map<String,Object>> records,List<MCol> cols,String title,Map<String,Object> footer) throws DataTableException{
		Table table = new Table();
		table.setId(IdsAndCssClassName.getComponentId(table));
		
		/*
		 * 若列信息为空，则返回空的Table。
		 */ 
		if(cols == null || cols.size() == 0){
			return table;
		}
		
		/*
		 * 若 表标题 不为空，则添加表名。
		 */
		if(StringUtil.isNotEmpty(title)){
			Caption cap = new Caption();
			Word word_title = new Word();
			word_title.setValue(title);
			cap.setValue(word_title);
			cap.setId(IdsAndCssClassName.getComponentId(cap));
			table.setTitle(cap);
		}
		
		/*
		 * 初始化 表头
		 */
		THead head = new THead();
		head.setId(IdsAndCssClassName.getComponentId(head));
		/*
		 * 初始化 地脚
		 */
		TFoot foot = new TFoot();
		foot.setId(IdsAndCssClassName.getComponentId(foot));
		
		/*
		 * 按照列的顺序，对列定义进行排序
		 */
		Comparator<MCol> comparator = new ColsComparator();
		Collections.sort(cols, comparator);
		
		/*
		 * 添加表头、地脚
		 */
		for (Iterator<MCol> iterator = cols.iterator(); iterator.hasNext();) {
			MCol col =  iterator.next();
			String showName = col.getShowName();
			String id = col.getId();
			Th th = new Th();
			Word word_th = new Word();
			word_th.setValue(showName);
			th.setId(IdsAndCssClassName.generateCellId(th,id));
			th.setValue(word_th);
			head.putATh(th);
			
			Td td = new Td();
			td.setId(IdsAndCssClassName.generateCellId(td, id));
			Object o = footer.get(id);
			td.setValue(getComponent(col, o));
			foot.putATd(td);
		}
		
		/*
		 * 初始化 表主体
		 */
		TBody body = new TBody();
		body.setId(IdsAndCssClassName.getComponentId(body));
		
		/*
		 * 添加 表主体
		 */
		for (Iterator<Map<String,Object>> iterator = records.iterator(); iterator.hasNext();) {
			Tr tr = new Tr();
			tr.setId(IdsAndCssClassName.getComponentId(tr));
			Map<String,Object> record = iterator.next();
			for (Iterator<MCol> iterator2 = cols.iterator(); iterator2.hasNext();) {
				MCol col = iterator2.next();
				String idToShow = col.getId();
				Td td = new Td();
				td.setId(IdsAndCssClassName.generateCellId(td, idToShow));
				Object o = record.get(idToShow);
				td.setValue(getComponent(col,o));
				tr.putATd(td);
			}
			body.putATr(tr);
		}
		
		table.setHead(head);
		table.setBody(body);
		table.setFoot(foot);
		return table;
	}
	
	private static WebComponents getComponent (MCol col, Object o){
		
	
		// TODO 
//		if(o instanceof String){
			Word word = new Word();
			word.setValue((String)o);
			return word;
//		}
	}
	
	
}
